
#balance table

age_bal_pr <- lm_robust(player.age ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
age_bal_mr <- lm_robust(player.age ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

educ_hi_bal_pr <- lm_robust(player.education == 4 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
educ_hi_bal_mr <- lm_robust(player.education == 4 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

educ_med_bal_pr <- lm_robust(player.education == 3 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
educ_med_bal_mr <- lm_robust(player.education == 3 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

educ_low_bal_pr <- lm_robust(player.education %in% c(1,2) ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
educ_low_bal_mr <- lm_robust(player.education %in% c(1,2) ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

gender_bal_pr <- lm_robust(player.gender == 1 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
gender_bal_mr <- lm_robust(player.gender == 1 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

islam_bal_pr <- lm_robust(player.religion == 1 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
islam_bal_mr <- lm_robust(player.religion == 1 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

christian_bal_pr <- lm_robust(player.religion %in% c(2, 3) ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
christian_bal_mr <- lm_robust(player.religion %in% c(2, 3) ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

other_relg_bal_pr <- lm_robust(player.religion %in% c(4,5,6) ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
other_relg_bal_mr <- lm_robust(player.religion %in% c(4,5,6) ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

javanese_bal_pr <- lm_robust(player.ethnicity == 1 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
javanese_bal_mr <- lm_robust(player.ethnicity == 1 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

sunda_bal_pr <- lm_robust(player.ethnicity == 2 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
sunda_bal_mr <- lm_robust(player.ethnicity == 2 ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)

other_eth_bal_pr <- lm_robust(player.ethnicity %in% c(3,4, 5,6,7,8,10) ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "patronage")), clusters = cluster)
other_eth_bal_mr <- lm_robust(player.ethnicity %in% c(3,4, 5,6,7,8,10) ~ group.treatment == "random", data = main_df %>% filter(group.treatment %in% c("random", "merit")), clusters = cluster)


pr_pvalues <- list(age_bal_pr, educ_hi_bal_pr, educ_med_bal_pr, educ_low_bal_pr, gender_bal_pr, islam_bal_pr, christian_bal_pr, other_relg_bal_pr, javanese_bal_pr, sunda_bal_pr, other_eth_bal_pr)
mr_pvalues <- list(age_bal_mr, educ_hi_bal_mr, educ_med_bal_mr, educ_low_bal_mr, gender_bal_mr, islam_bal_mr, christian_bal_mr, other_relg_bal_mr, javanese_bal_mr, sunda_bal_mr, other_eth_bal_mr)

get_pvalues <- function(reg){
  reg %>%
    tidy() %>%
    select(p.value) %>%
    slice(2) %>%
    as.character()
}

pr_p <-lapply(pr_pvalues, get_pvalues) %>% unlist()
mr_p <-lapply(mr_pvalues, get_pvalues) %>% unlist()

#get averages for table and make it

bal_data <-
  main_df %>%
  group_by(group.treatment) %>%
  summarise(age = mean(player.age, na.rm = T),
            educ_college = mean(player.education == 4, na.rm = T),
            educ_hs  = mean(player.education == 3, na.rm = T),
            educ_less_hs  = mean(player.education %in% c(1, 2), na.rm = T),
            male = mean(player.gender == 1, na.rm = T),
            relg_islam = mean(player.religion  == 1, na.rm = T),
            relg_chrs = mean(player.religion %in% c(2, 3), na.rm = T),
            relg_other = mean(player.religion %in% c(4, 5, 6), na.rm = T),
            eth_java = mean(player.ethnicity == 1, na.rm = T),
            eth_sunda = mean(player.ethnicity == 2, na.rm = T),
            eth_other = mean(!(player.ethnicity %in% c(1, 2)), na.rm = T)) %>%
  t() %>%
  data.frame() %>%
  slice(2:nrow(.)) %>%
  rownames_to_column() %>%
  set_colnames(c("var", "patronage", "random", "merit")) %>%
  select("var", "random", "patronage", "merit") %>%
  mutate(rand_min_patr = as.numeric(random)-as.numeric(patronage),
         rand_min_merit = as.numeric(random)-as.numeric(merit)) %>%
  bind_cols(pval_pr = pr_p,
            pval_mr = mr_p)


bal_data %>%
  mutate_at(vars(random:pval_mr), funs(round(as.numeric(.), digits = 3))) %>%
  mutate(var = case_when(var == "age" ~ "Age",
                         var == "educ_college" ~ "Education: College",
                         var == "educ_hs" ~ "Education: HS",
                         var == "educ_less_hs" ~ "Education: Less than HS",
                         var == "male" ~ "Gender: Man",
                         var == "relg_islam" ~ "Religion: Islam",
                         var == "relg_chrs" ~ "Religion: Christian",
                         var == "relg_other" ~ "Religion: Other",
                         var == "eth_java" ~ "Ethnicity: Javanese",
                         var == "eth_sunda" ~ "Ethnicity: Sunda",
                         var == "eth_other" ~ "Ethnicity: Other")) %>%
  gt() %>%
  tab_header(title = "Descriptive Statistics and Balance Tests") %>%
  as_latex() %>%
  cat(., file = "./outputs/tables/table_a2.tex")

